﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ArgusLib.Fractals.OrbitFractals;
using ArgusLib.Drawing;

namespace Fraktalysator.Controls
{
	public partial class FractalMandelbrotSetInputControl : UserControl, IFractalParameterInputControl
	{
		public FractalMandelbrotSetInputControl()
		{
			InitializeComponent();
			this.nudIterations.Maximum = int.MaxValue;
			this.Dock = DockStyle.Fill;
		}

		public Parameter Parameter
		{
			get
			{
				Parameter para = new Parameter((int)this.nudPixelWidth.Value, (int)this.nudPixelHeight.Value);
				SizeD size = new SizeD(this.ntbShownWidth.Value, this.ntbShownHeight.Value);
				size = size.ChangeRatio(para.PixelWidth / (double)para.PixelHeight, ChangeRatioOptions.IncreaseAreaSize);
				para.Iterations = (int)this.nudIterations.Value;
				para.ShownArea = new ComplexArea()
				{
					 CenterRe = this.ntbCenterRe.Value,
					 CenterIm = this.ntbCenterIm.Value,
					 Width = size.Width,
					 Height = size.Height,
					 Rotation = ArgusLib.Math.Convert.DegToRad((double)this.nudRotation.Value)
				};
				para.ThreadCount = (int)this.nudThreads.Value;
				return para;
			}
			set
			{
				this.nudPixelWidth.Value = value.PixelWidth;
				this.nudPixelHeight.Value = value.PixelHeight;
				this.nudIterations.Value = value.Iterations;
				this.ntbCenterRe.Text = value.ShownArea.CenterRe.ToString();
				this.ntbCenterIm.Text = value.ShownArea.CenterIm.ToString();
				this.ntbShownWidth.Text = value.ShownArea.Width.ToString();
				this.ntbShownHeight.Text = value.ShownArea.Height.ToString();
				double rot = value.ShownArea.Rotation;
				while (rot < 0)
					rot += 2 * Math.PI;
				while (rot > 2 * Math.PI)
					rot -= 2 * Math.PI;
				this.nudRotation.Value = (decimal)ArgusLib.Math.Convert.RadToDeg(rot);
				this.nudThreads.Value = value.ThreadCount;
			}
		}

		ArgusLib.Fractals.IParameter IFractalParameterInputControl.Parameter { get { return this.Parameter; } set { this.Parameter = (Parameter)value; } }

		public Control Control { get { return this; } }
	}
}
